<template>
  <header class="navbar">
    <!-- 左侧Logo和标题 -->
    <div class="navbar-left">
      <img src="@/assets/logo.svg" alt="Logo" class="logo" />
      <h1 class="title">{{ title }}</h1>
    </div>

    <!-- 中间导航链接 -->
    <nav class="navbar-center">
      <router-link
        v-for="item in navLinks"
        :key="item.path"
        :to="item.path"
        class="nav-link"
        active-class="active"
      >
        {{ item.label }}
      </router-link>
    </nav>

    <!-- 右侧用户菜单 -->
    <div class="navbar-right">
      <div class="user-menu" @click="toggleUserMenu">
        <div class="user-info">
          <span class="user-icon">👤</span>
          <span class="username">{{ username }}</span>
          <span class="dropdown-arrow">{{ userMenuOpen ? '▲' : '▼' }}</span>
        </div>
        <div v-if="userMenuOpen" class="user-dropdown">
          <div v-for="item in userMenuItems" :key="item.label" class="dropdown-item" @click="handleMenuItemClick(item)">
            {{ item.label }}
          </div>
        </div>
      </div>
    </div>
  </header>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';

// Props定义
interface Props {
  title?: string;
  username?: string;
  navLinks?: Array<{
    label: string;
    path: string;
  }>;
  userMenuItems?: Array<{
    label: string;
    action?: () => void;
  }>;
}

// 默认属性值
const props = withDefaults(defineProps<Props>(), {
  title: '导航系统',
  username: '用户',
  navLinks: () => [
    { label: '首页', path: '/' },
    { label: '关于', path: '/about' },
    { label: '服务', path: '/services' },
    { label: '联系我们', path: '/contact' }
  ],
  userMenuItems: () => [
    { label: '个人资料' },
    { label: '设置' },
    { label: '退出登录' }
  ]
});

// 定义emit事件
const emit = defineEmits<{
  (e: 'menu-item-click', item: { label: string; action?: () => void }): void;
}>();

// 用户菜单状态
const userMenuOpen = ref(false);

// 切换用户菜单
const toggleUserMenu = () => {
  userMenuOpen.value = !userMenuOpen.value;
};

// 处理菜单项点击
const handleMenuItemClick = (item: { label: string; action?: () => void }) => {
  if (item.action) {
    item.action();
  }
  emit('menu-item-click', item);
  userMenuOpen.value = false;
};

// 点击页面其他地方关闭菜单
const handleClickOutside = (event: MouseEvent) => {
  const target = event.target as HTMLElement;
  if (!target.closest('.user-menu')) {
    userMenuOpen.value = false;
  }
};

// 生命周期钩子
onMounted(() => {
  document.addEventListener('click', handleClickOutside);
});

onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside);
});
</script>

<style scoped>
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  background-color: #ffffff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  height: 60px;
  position: sticky;
  top: 0;
  z-index: 100;
}

/* 左侧Logo和标题 */
.navbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo {
  height: 40px;
  width: auto;
}

.title {
  font-size: 18px;
  margin: 0;
  color: #333333;
  font-weight: 600;
}

/* 中间导航链接 */
.navbar-center {
  display: flex;
  gap: 24px;
}

.nav-link {
  color: #666666;
  text-decoration: none;
  padding: 8px 12px;
  border-radius: 4px;
  transition: all 0.3s ease;
  font-size: 14px;
}

.nav-link:hover {
  color: #1677ff;
  background-color: #f5f5f5;
}

.nav-link.active {
  color: #1677ff;
  font-weight: 500;
  background-color: #e6f4ff;
}

/* 右侧用户菜单 */
.navbar-right {
  position: relative;
}

.user-menu {
  position: relative;
  cursor: pointer;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.3s ease;
}

.user-info:hover {
  background-color: #f5f5f5;
}

.user-icon {
  font-size: 20px;
}

.username {
  color: #333333;
  font-weight: 500;
  font-size: 14px;
}

.dropdown-arrow {
  font-size: 12px;
  color: #666666;
  transition: transform 0.3s ease;
}

.user-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 4px;
  background-color: #ffffff;
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  min-width: 160px;
  z-index: 101;
  overflow: hidden;
}

.dropdown-item {
  padding: 12px 16px;
  color: #333333;
  font-size: 14px;
  transition: background-color 0.3s ease;
  cursor: pointer;
}

.dropdown-item:hover {
  background-color: #f5f5f5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .navbar-center {
    display: none;
  }
  
  .title {
    font-size: 16px;
  }
  
  .navbar {
    padding: 0 16px;
  }
}

@media (max-width: 480px) {
  .navbar-left {
    gap: 8px;
  }
  
  .logo {
    height: 32px;
  }
  
  .title {
    font-size: 14px;
  }
  
  .username {
    display: none;
  }
}
</style>